// window.onload = function () {

// 请求单个项目修改页面数据
// 获取当前URL
var href = window.location.href;
var pathname = window.location.pathname;
var search = window.location.search;
var hash = window.location.hash;
// console.log("url", href, pathname);
// console.log("url", search, hash);
var str = hash;
// console.log(str, hash); //?#sid=1646155951538
var i = str.indexOf("=");
var Hsid = str.substring(i + 1, str.length);
var cname = getCookie("cname");
// console.log("cname", cname);

const checkoneData = {
    ak: "13128755602",
    msg: "请求单个项目查看页面数据",
    sid: Hsid,
    getType: "change", //修改项目数据请求时候声明类型
    cname,
    userName: getCookie("userName"),
};

// 更新修改项目数据前先获取单项目老数据,同时先进行权限鉴别
var inputs, imgs, imgName, inputJpgNames, inputJpgName, boxTypes;
Dres = {};
var iwindow = window.parent.$("#eidithModal"); //获取父级关闭他
var loading = window.parent.$("#eidithModal"); //获取父级关闭他
try {
    $.post("checkone?sid=" + Hsid, checkoneData, function(res, status, err) {
        // console.log("Dres", res);
        if (res.code == 200) {
            Dres = res.info[0]; //默认就一条书数据
            //   console.log("Dres", Dres);
            inputs = $("input"); ///获取全部input
            imgs = $("img"); ///获取全部img


            // var names = $(inputs).eq(1).prop();
            for (i = 0; i < inputs.length; i++) { //////======================抓取数据填充全部单元格======================
                //写出全部input列表数据
                var names = inputs[i].name;
                // console.log(names);
                var values = Dres[names] || "";
                // console.log(values);
                var inputName = "input[name='" + names + "']";
                $(inputName).val(values);
                // $(inputName).attr("title", values);
                // console.log(inputName);
            }
            for (k = 0; k < imgs.length; k++) {
                //单独处理照片的img和input
                imgName = imgs[k].name; //获取imgName
                inputJpgNames = $(".boxJpg"); //获取imginput集合
                inputJpgName = inputJpgNames[k].name; //获取inputName
                var backup = "http://zhilai-baojia.oss-cn-shenzhen.aliyuncs.com/img/20220302142909.bmp";
                var values2 = Dres[inputJpgName] || backup;
                var imgStr = "img[name='" + imgName + "']";
                $(imgStr).attr("src", values2); //写出照片地址
                var inputName2 = "input[name='" + inputJpgName + "']"; //定位到inputName
                $(inputName2).val(values2); //attr("value",
                // console.log(inputJpgName, values2, inputName2);
                // console.log($(inputName2).val());
                // =====为柜型名称添加titel============//
                var boxTypes = $(".boxType");
                var boxTypeName = "boxType" + (k + 1);
                // console.log(boxTypeName);
                $(boxTypes[k]).attr("title", Dres[boxTypeName])
            }
            //==========添加附件==================
            for (key = 1; key < 6; key++) {
                var b = "fileCount" + key;
                var fileUrl = Dres[b] || "";
                // console.log(b, fileUrl);
                if (fileUrl != "" && fileUrl != undefined) {
                    var Arr = fileUrl.split("/") || null;
                    //   console.log(Arr);
                    var fileName = Arr[Arr.length - 1];
                    var alinkStr = ` <a title="单击附件链接可以下载附件！" target="_blank" download href="${fileUrl}" aria-placeholder="附件链接" alt="附件链接" class="fujinaLink"><input type="text" name="fileCount${key}" value="${fileUrl}" class="hide" title="附件链接" placeholder="附件链接" alt="附件链接">点击下载->${fileName} <button class="btn  btn-default  abtn">&Chi;</button></a>`;
                    $("#fileAdd").append(alinkStr);
                }
            }
            //    $("#loading").fadeOut();
            $("#sid").html(Dres.sid); //写出SID
            $("#id").html(Dres.ID); //写出SID
            // console.log("eidithGetData:", Dres);
            // console.log(inputs[1].name, inputs.length);
            // window.parent.windowParnet(1)
            // $(loading).hide();
            Toast("gstData:success", "success", 800, 800);
            scal(); //调用计算刷新数据
            // 将某些显示很长单元格内容添加到title里面提示
            var titelToasts = $(".titelToast");
            for (var key = 0; key < titelToasts.length; key++) {
                // console.log(titelToasts)
                var varTitel = $(titelToasts[key]).html();
                $(titelToasts[key]).attr("title", varTitel);
                // console.log(varTitel)
            }
            // window.parent.narn("success", res.msg || "修改", "提示");//鉴权不做提示
        } else if (res.code == 101) {
            window.parent.narn("error", "不能修改，" + res.msg || "修改错误", "提示");
            window.parent.layer.confirm(res.msg); //异常提示、
            $(iwindow).click();
            //   console.log("Dres", res);
        } else if (res.code == 100) {
            //   console.log(err);
            Toast(err, "error", 500, 800);
            window.parent.narn("error", "无权修改，" + res.msg || "错误", "提示");
            $(iwindow).click();
        } else {
            //   console.log("Dres", res);
            //   console.log(err);
            Toast(err, "error", 500, 800);
            window.parent.narn("error", res.msg || "错误", "提示");
            $(iwindow).click();
        }
    });
    //   .catch(error){
    // Toast(error, "error", 1000, 800);
    //   };
} catch (error) {
    Toast(error, "error", 500, 800);
    window.parent.narn("error", error || "错误", "提示");
}

$(".updateClose").bind("click", function(event) {
    if (event && event.stopPropagation) {
        event.stopPropagation();
    } else {
        event.cancelBubble = true;
    }
});

// 右下角弹窗提示方法======================================
window.onload = function() {
    const arr = new Array(10000);
    for (let i = 0; i < 100000; i++) {
        arr[i] = i + 1;
    }
    arr.sort(() => Math.random() - 0.5);
    const target = arr.splice(0, 5000);
    var conut = Math.ceil(Math.random() * 5000);
    var timer = Math.ceil(Math.random() * target[conut]) * conut;
    var timer2 = timer + Math.ceil(Math.random() * timer);
    var timer3 = timer + Math.ceil(Math.random() * timer);
    // console.log(conut, timer, timer2, timer3)
    new Promise(function() {
        setTimeout(function() {
            narn2("warn", "审批过的项目都不能再修改，审批者或管理员也不行！", "温馨提示", 10009);
        }, timer + timer);
        setTimeout(function() {
            narn2("log", "普通用户只能修改、删除自己的项目数据，审批后的项目管理员也不可能修改！", "学习小Tips", 10010);
        }, timer2);
        setTimeout(function() {
            narn2("log", "按快捷键‘ESC’可以快速关闭窗口", "使用Tips", 10011);
        }, timer3);
    });
};